package com.spring.propertyioc;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

//继承PropertyPlaceholderConfigurer，定义支持密文版属性的属性配置器
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private String[] encryptPropNames = {"userName","password"};

    //对特定属性的属性值进行转换,将加密的属性值进行解密
    @Override
    protected String convertProperty(String propertyName, String propertyValue) {

        if(isEncryptProp(propertyName)){
            String decryptValue = DESUtils.getDecryptString(propertyValue);
            System.out.println("原始值：" + propertyValue + ",解密后的值：" + decryptValue);
            return  decryptValue;
        }
        return super.convertProperty(propertyName, propertyValue);
    }

    //判断是否是需要解密的属性
    private boolean isEncryptProp(String propertyName){
        for (String str:encryptPropNames) {
            if(propertyName.equals(str)){
                return true;
            }
        }
        return false;
    }
}
